package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.feign.AdminFeign;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.WmSensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-01-17
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    @Autowired
    private AdminFeign adminFeign;
    /**
     * 新增敏感词
     * @param wmSensitive
     */
    @Override
    public void saveSensitive(WmSensitive wmSensitive) {
        WmSensitive wmSensitive1 = new WmSensitive();
        BeanUtils.copyProperties(wmSensitive, wmSensitive1);
        this.save(wmSensitive1);
    }

    /**
     * 查询敏感词
     * @param wmSensitiveDto
     * @return
     */
    @Override
    public ResponseResult findSensitive(WmSensitiveDto wmSensitiveDto) {
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (wmSensitiveDto.getName() != null) {
            queryWrapper.like(WmSensitive::getSensitives, wmSensitiveDto.getName());
        }
        //执行分页查询
        IPage<WmSensitive> page = this.page(new Page<>(wmSensitiveDto.getPage(), wmSensitiveDto.getSize()), queryWrapper);
        return new PageResponseResult(wmSensitiveDto.getPage(), wmSensitiveDto.getSize(), page.getTotal(), page.getRecords());

    }

    /**
     * 修改敏感词
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        Integer sensitiveId = wmSensitive.getId();
        WmSensitive wmSensitive1 = this.getById(sensitiveId);
        BeanUtils.copyProperties(wmSensitive, wmSensitive1);
        this.updateById(wmSensitive1);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deketeSensitive(Integer id) {
        this.removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult saveSensitive(SensitiveDto dto) {
        //查询数据库中是否存在相同的敏感词
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives, dto.getSensitives());
        WmSensitive wmSensitive = this.getOne(queryWrapper);
        if (wmSensitive != null) {
            //敏感词已经存在返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //敏感词不存在,保存敏感词
        WmSensitive sensitive = new WmSensitive();
        BeanUtils.copyProperties(dto, sensitive);
        this.save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词列表模糊查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(SensitiveDto dto) {
        //判断当前传递的值中是否包含name属性
        if (StringUtils.isBlank(dto.getName())) {
            //设置关键字排序
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
            //没有模糊字，执行分页查询全部敏感字
            Page<WmSensitive> page = this.page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);
            //构造返回值返回
            return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        } else {
            //模糊字存在,根据模糊字查询
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(WmSensitive::getSensitives, dto.getName());
            queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
            Page<WmSensitive> page = this.page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);
            //构造返回值返回
            return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        }
    }

    /**
     * 更新敏感词实体信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(SensitiveDto dto) {
        try {
            if (StringUtils.isBlank(dto.getSensitives()) && StringUtils.isBlank(dto.getId().toString())) {
                //不存在要更新的敏感词和要更新的敏感词id直接抛出异常
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            } else {
                //根据敏感词查询。看数据库中是否存在要修改的敏感词
                LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(WmSensitive::getSensitives, dto.getSensitives());
                WmSensitive wmSensitive = this.getOne(queryWrapper);
                if (wmSensitive != null) {
                    //数据库中已经存在相同的敏感词。不允许修改成已有敏感词
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
                //根据敏感词的id查询得到敏感词修改前的词是什么
                WmSensitive sensitive = this.getById(dto.getId());
                dto.setUpdateSensitives(sensitive.getSensitives());
                //更新敏感词信息根据敏感词id
                LambdaUpdateWrapper<WmSensitive> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(WmSensitive::getId, dto.getId());
                updateWrapper.set(WmSensitive::getSensitives, dto.getSensitives());
                this.update(updateWrapper);
                //远程调用feign接口，更新后台端的敏感词信息
                adminFeign.updateSensitive(dto);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("更新敏感词信息错误：{}" + e.getMessage());
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 删除敏感词实体信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        try {
            //判断当前id的敏感词是否存在
            WmSensitive sensitive = this.getById(id);
            if (sensitive == null) {
                //当前敏感词不存在。直接返回
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
            SensitiveDto dto = new SensitiveDto();
            BeanUtils.copyProperties(sensitive,dto);
            //远程调用feign接口，删除后台端的敏感词信息
            adminFeign.deleteSensitive(dto);
            //当前id的敏感词存在。直接删除
            this.removeById(id);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除敏感词信息错误：{}" + e.getMessage());
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
